/**
 * Created by xiebang
 * Description
 * User:小谢
 * Date:2021-08-06
 * Time:9:30
 */

class Node{
    public int val;
    public Node next;

    public Node(int val) {
        this.val = val;
    }
}
public class myLinkedList {
    public Node head;//标识单链表的头结点

    /**
     * 穷举的方式创建链表，当然很low,方便理解
     */
    public void createList(){
        Node node1 = new Node(12);
        Node node2 = new Node(3);
        Node node3 = new Node(5);
        Node node4 = new Node(2);
        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = null;
        this.head = node1;
    }

    //打印单链表
    public void show() {
        Node cur = this.head;
        while (cur!=null) {
            System.out.print(cur.val + " -> ");
            cur = cur.next;
        }
        System.out.println("null");
    }

    // 得到单链表的长度
    public int size() {
        if (this.head == null) {
            System.out.println("链表为空");
            return 0;
        }
        Node cur = this.head;
        int count = 0;
        while (cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }

    //查找是否包含关键字key是否在单链表
    public boolean contains (int key) {
        if(this.head == null) {
            System.out.println("插入链表为空");
            return false;
        }
        Node cur = this.head;
        while (cur != null) {
            if(cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    //头插法
    public void addFirst(int data) {
        Node newNode = new Node(data);
        if (this.head == null)  {
            this.head = newNode;
        }else {
            newNode.next = this.head;
            this.head = newNode;
        }
    }
    //尾插法
    public void addLast(int data) {
        Node newNode = new Node(data);
        if (this.head == null) {
            this.head = newNode;
        }else {
            Node cur = this.head;
            while (cur.next != null) {
                cur = cur.next;
            }
            cur.next = newNode;
        }
    }
    public Node searchPrev(int index) {
        Node cur = this.head;
        int count = 0;
        while (count != index - 1) {
            cur = cur.next;
            count++;
        }
        return cur;
    }
    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index,int data){

//        Node cur = this.head;
//        Node perv = null;
        if (index < 0 || index > size()) {

            System.out.println("位置不合法");
            return;
        }
        //插入第一个节点
        if(index == 0) {
            addFirst(data);
            return;
        }
        if (index == size()) {
            addLast(data);
            return;
        }
        Node newNode = new Node(data);
        Node cur = searchPrev(index);
        newNode.next = cur.next;
        cur.next = newNode;

//        else {
//            while (index > 0) {
//                if (index > 0 && cur == null) {
//                    System.out.println("坐标非法");
//                    return;
//                }
//                perv = cur;
//                cur = cur.next;
//                index--;
//            }
//            perv.next = newNode;
//            newNode.next = cur;
//        }
    }

    //删除第一次出现关键字为key的节点
    public Node searchPrevNode(int val) {
        if(contains(val)) {
            Node cur = this.head;
            if (cur.val == val) {
                return null;
            }
            while (cur.next != null) {
                if (cur.next.val == val) {
                    return cur;
                }
                cur = cur.next;
            }
        }
        return null;
    }
    public void remove(int key) {
        if (this.head == null) {
            return;
        }
        Node prev = searchPrevNode(key);
        if (prev == null) {
            if(this.head.val == key) {
                this.head = this.head.next;
                return;
            }
        }else {
            prev.next = prev.next.next;
        }
//        if (this.contains(key)) {
//            Node cur = this.head;
//            Node prev = null;
//            //在第一个节点上。
//            if (this.head.val == key) {
//                this.head = this.head.next;
//            } else {
//                while (cur.next != null && cur.next.next != null) {
//                    prev = cur;
//                    if (cur.next.val == key) {
//                        prev.next = cur.next.next;
//                        return;
//                    }
//                    cur = cur.next;
//                }
//                //当数据在最后一个的情况
//                cur.next = null;
//            }
//        }else {
//            System.out.println("没有该数字");
//        }
    }
    //删除所有值为key的节点
    public void removeAllKey(int key) {
        if(this.head == null) {
            return;
        }
        Node cur = this.head.next;
        Node prev = this.head;
        while (cur != null) {
            if (cur.val == key) {
                prev.next = cur.next;
                cur = cur.next;
            } else {
                prev = cur;
                cur = cur.next;
            }
        }
        if(this.head.val == key) {
            this.head = this.head.next;
        }
    }
}
